﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BO.Admin;

namespace ProjectQuaDep.Admin.Company
{
    public partial class ListCompany : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ShowAll();
            }
        }

        protected void ShowAll()
        {
            string search = txtSearch.Text.Trim();
            string checkStatus = rbCheckFind.SelectedValue.ToString();
            DataTable dt = CompanyBO.GetAllCompanyBO(search, checkStatus);
            gvShow.DataSource = dt;
            gvShow.DataBind();
        }
        protected void cmdUpdate_Click(object sender, EventArgs e)
        {
            if (cmdUpdate.Text == "Thêm mới")
            {
                //them moi
                int kq = CompanyBO.AddCompanyBO(txtTitle.Text,txtWeb.Text,BO.Utils.getOneSelect(ckSelect),txtDesc.Text,txtEmail.Text,txtImages.Text);
                if (kq == 1)
                {
                    lblMs.Text = "";
                    pnUpdate.Visible = false;
                    ShowAll();
                }
                else
                {
                    lblMs.Text = "Thêm mới thất bại";
                }
            }
            else
            {
                //sua chua
                int kq = CompanyBO.EditCompanyBO(txtTitle.Text, txtWeb.Text, BO.Utils.getOneSelect(ckSelect), txtDesc.Text, txtEmail.Text, txtImages.Text,lblID.Text);
                if (kq == 1)
                {
                    lblMs.Text = "";
                    pnUpdate.Visible = false;
                    ShowAll();
                }
                else
                {
                    lblMs.Text = "Sửa chữa thất bại";
                }
            }
        }

        protected void cmdHide_Click(object sender, EventArgs e)
        {
            pnUpdate.Visible = false;
        }

        protected void gvShow_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvShow.PageIndex = e.NewPageIndex;
            lblPage.Text = "Trang: " + (gvShow.PageIndex + 1).ToString();
            ShowAll();
        }

        protected void gvShow_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string cmdName = e.CommandName.ToString();
            string ID = e.CommandArgument.ToString();
            if (cmdName == "cmdEdit")
            {
                pnUpdate.Visible = true;
                DataTable dt = BO.Admin.CompanyBO.GetInfomationBO(ID);
                if (dt.Rows.Count > 0)
                {
                    cmdUpdate.Text = "Sửa Chữa";
                    pnUpdate.Visible = true;
                    lblID.Text = ID;
                    txtDesc.Text = dt.Rows[0][BO.Const.tbCompanies.Note].ToString();
                    txtEmail.Text = dt.Rows[0][BO.Const.tbCompanies.Email].ToString();
                    txtImages.Text = dt.Rows[0][BO.Const.tbCompanies.Logo].ToString();
                    txtTitle.Text = dt.Rows[0][BO.Const.tbCompanies.NameCp].ToString();
                    txtWeb.Text = dt.Rows[0][BO.Const.tbCompanies.Website].ToString();
                    BO.Utils.setOneSelect(ckSelect, dt.Rows[0][BO.Const.tbCompanies.Status].ToString());
                }
            }
        }

        protected void cmdSearch_Click(object sender, ImageClickEventArgs e)
        {
            ShowAll();
        }

        protected void cmdAdd_Click(object sender, ImageClickEventArgs e)
        {
            pnUpdate.Visible = true;
            cmdUpdate.Text = "Thêm mới";
            txtDesc.Text = "";
            txtEmail.Text = "";
            txtImages.Text = "";
            txtTitle.Text = "";
            txtWeb.Text = "";
            ckSelect.Checked = false;
        }

        protected void rbCheckFind_SelectedIndexChanged(object sender, EventArgs e)
        {
            ShowAll();
        }

        protected void ddlZoneFind_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}